package com.ss.android.common.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.CompoundButton;
import com.ss.android.common.dd;

public class MySwitch extends CompoundButton {
    private Drawable a;
    private Drawable b;
    private int c;
    private int d;
    private int e;
    private int f;
    private float g;
    private float h;
    @SuppressLint(value={"Recycle"}) private VelocityTracker i;
    private int j;
    private float k;
    private int l;
    private int m;
    private int n;
    private int o;
    private int p;
    private int q;
    private int r;
    private final Rect s;
    private static final int[] t;

    static {
        t = new int[]{16842912};
    }

    public MySwitch(Context arg2) {
        this(arg2, null);
    }

    public MySwitch(Context arg2, AttributeSet arg3) {
        this(arg2, arg3, 0);
    }

    public MySwitch(Context arg4, AttributeSet arg5, int arg6) {
        super(arg4, arg5, arg6);
        this.i = VelocityTracker.obtain();
        this.s = new Rect();
        TypedArray v0 = arg4.obtainStyledAttributes(arg5, dd.f, arg6, 0);
        this.a = v0.getDrawable(dd.i);
        this.b = v0.getDrawable(dd.j);
        this.c = v0.getDimensionPixelSize(dd.g, 0);
        this.d = v0.getDimensionPixelSize(dd.h, 0);
        v0.recycle();
        ViewConfiguration v0_1 = ViewConfiguration.get(arg4);
        this.f = v0_1.getScaledTouchSlop();
        this.j = v0_1.getScaledMinimumFlingVelocity();
        this.refreshDrawableState();
        this.setChecked(this.isChecked());
    }

    protected void a(boolean arg1) {
        this.setChecked(arg1);
    }

    protected void drawableStateChanged() {
        super.drawableStateChanged();
        int[] v0 = this.getDrawableState();
        if(this.a != null) {
            this.a.setState(v0);
        }

        if(this.b != null) {
            this.b.setState(v0);
        }

        this.invalidate();
    }

    public int getCompoundPaddingLeft() {
        int v0 = super.getCompoundPaddingLeft() + this.l;
        if(!TextUtils.isEmpty(this.getText())) {
            v0 += this.d;
        }

        return v0;
    }

    public int getCompoundPaddingRight() {
        int v0 = super.getCompoundPaddingRight() + this.l;
        if(!TextUtils.isEmpty(this.getText())) {
            v0 += this.d;
        }

        return v0;
    }

    private boolean getTargetCheckedState() {
        boolean v0 = this.k >= (((float)(this.getThumbScrollRange() / 2))) ? true : false;
        return v0;
    }

    public Drawable getThumbDrawable() {
        return this.a;
    }

    private int getThumbScrollRange() {
        int v0;
        if(this.b == null) {
            v0 = 0;
        }
        else {
            this.b.getPadding(this.s);
            v0 = this.l - this.n - this.s.left - this.s.right;
        }

        return v0;
    }

    public Drawable getTrackDrawable() {
        return this.b;
    }

    protected int[] onCreateDrawableState(int arg3) {
        int[] v0 = super.onCreateDrawableState(arg3 + 1);
        if(this.isChecked()) {
            mergeDrawableStates(v0, t);
        }

        return v0;
    }

    protected void onDraw(Canvas arg7) {
        super.onDraw(arg7);
        int v0 = this.o;
        int v1 = this.p;
        int v2 = this.q;
        int v3 = this.r;
        this.b.setBounds(v0, v1, v2, v3);
        this.b.draw(arg7);
        arg7.save();
        this.b.getPadding(this.s);
        v0 += this.s.left;
        arg7.clipRect(v0, v1, v2 - this.s.right, v3);
        this.a.getPadding(this.s);
        v2 = ((int)(this.k + 0.5f));
        this.a.setBounds(v0 - this.s.left + v2, v1, v0 + v2 + this.n + this.s.right, v3);
        this.a.draw(arg7);
        arg7.restore();
    }

    protected void onLayout(boolean arg5, int arg6, int arg7, int arg8, int arg9) {
        int v0;
        int v1;
        super.onLayout(arg5, arg6, arg7, arg8, arg9);
        this.setThumbPosition(this.isChecked());
        int v2 = this.getWidth() - this.getPaddingRight();
        int v3 = v2 - this.l;
        switch(this.getGravity() & 112) {
            case 16: {
                v1 = (this.getPaddingTop() + this.getHeight() - this.getPaddingBottom()) / 2 - this.m / 2;
                v0 = this.m + v1;
                break;
            }
            case 80: {
                v0 = this.getHeight() - this.getPaddingBottom();
                v1 = v0 - this.m;
                break;
            }
            default: {
                v1 = this.getPaddingTop();
                v0 = this.m + v1;
                break;
            }
        }

        this.o = v3;
        this.p = v1;
        this.r = v0;
        this.q = v2;
    }

    public void onMeasure(int arg4, int arg5) {
        int v0 = Math.max(this.c, this.b.getIntrinsicWidth());
        int v1 = this.b.getIntrinsicHeight();
        this.n = this.a.getIntrinsicWidth();
        this.l = v0;
        this.m = v1;
        this.setMeasuredDimension(v0, v1);
    }

    public boolean onTouchEvent(MotionEvent arg11) {
        float v3_1;
        float v0_1;
        int v0;
        int v6 = 2;
        int v9 = 3;
        boolean v2 = false;
        boolean v1 = true;
        this.i.addMovement(arg11);
        switch(arg11.getActionMasked()) {
            case 0: {
                this.getParent().requestDisallowInterceptTouchEvent(true);
                v3_1 = arg11.getX();
                float v4 = arg11.getY();
                if(!this.isEnabled()) {
                    if(this.e == 0) {
                        v1 = false;
                    }
                    else {
                    }
                    return v1;
                }

                this.a.getPadding(this.s);
                v0 = this.p - this.f;
                int v5 = this.o + (((int)(this.k + 0.5f))) - this.f;
                v6 = this.n + v5 + this.s.left + this.s.right + this.f;
                int v7 = this.r + this.f;
                v0 = v3_1 <= (((float)v5)) || v3_1 >= (((float)v6)) || v4 <= (((float)v0)) || v4 >= (((float)v7)) ? 0 : 1;
                this.e = v0 != 0 ? 1 : v9;
                this.g = v3_1;
                this.h = v4;
                if(this.e == 0) {
                    v1 = false;
                }
                else {
                }
                return v1;
            }
            case 2: {
                switch(this.e) {
                    case 1: {
                        v0_1 = arg11.getX();
                        v3_1 = arg11.getY();
                        if(Math.abs(v0_1 - this.g) <= (((float)this.f)) && Math.abs(v3_1 - this.h) <= (((float)this.f))) {
                        label_9:
                            if(this.e == 0) {
                                v1 = false;
                            }
                            else {
                            }
                            return v1;
                        }

                        this.e = v6;
                        this.g = v0_1;
                        this.h = v3_1;
                        return v1;
                    }
                    case 2: {
                        v0_1 = arg11.getX();
                        float v2_1 = Math.max(0f, Math.min(v0_1 - this.g + this.k, ((float)this.getThumbScrollRange())));
                        if(v2_1 == this.k) {
                            return v1;
                        }

                        this.k = v2_1;
                        this.g = v0_1;
                        this.invalidate();
                        return v1;
                    }
                    default: {
                        if(this.e == 0) {
                            v1 = false;
                        }
                        else {
                        }
                        return v1;
                    }
                }
            }
            case 1: 
            case 3: {
                if(this.e == v6) {
                    this.e = 0;
                    v0 = arg11.getAction() != 1 || !this.isEnabled() ? 0 : 1;
                    MotionEvent v3 = MotionEvent.obtain(arg11);
                    v3.setAction(v9);
                    super.onTouchEvent(v3);
                    v3.recycle();
                    if(v0 != 0) {
                        this.i.computeCurrentVelocity(1000);
                        v0_1 = this.i.getXVelocity();
                        if(Math.abs(v0_1) <= (((float)this.j))) {
                            v2 = this.getTargetCheckedState();
                        }
                        else if(v0_1 > 0f) {
                            v2 = true;
                        }

                        this.a(v2);
                        return v1;
                    }

                    this.a(this.isChecked());
                    return v1;
                }

                if(this.e != 1 && this.e != v9) {
                    this.e = 0;
                    this.i.clear();
                    if(this.e == 0) {
                        v1 = false;
                    }
                    else {
                    }
                    return v1;
                }

                this.e = 0;
                this.i.clear();
                this.toggle();
                break;
            }
            default: {
            }
        }

        return v1;
    }

    public void setChecked(boolean arg2) {
        super.setChecked(arg2);
        this.setThumbPosition(this.isChecked());
        this.invalidate();
    }

    public void setSwitchPadding(int arg1) {
        this.d = arg1;
        this.requestLayout();
    }

    public void setThumbDrawable(Drawable arg1) {
        this.a = arg1;
        this.requestLayout();
    }

    private void setThumbPosition(boolean arg2) {
        float v0 = arg2 ? ((float)this.getThumbScrollRange()) : 0f;
        this.k = v0;
    }

    public void setThumbResource(int arg2) {
        this.setThumbDrawable(this.getContext().getResources().getDrawable(arg2));
    }

    public void setTrackDrawable(Drawable arg1) {
        this.b = arg1;
        this.requestLayout();
    }

    public void setTrackResource(int arg2) {
        this.setTrackDrawable(this.getContext().getResources().getDrawable(arg2));
    }

    protected boolean verifyDrawable(Drawable arg2) {
        boolean v0 = (super.verifyDrawable(arg2)) || arg2 == this.a || arg2 == this.b ? true : false;
        return v0;
    }
}

